﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Project_CFT2.ViewModel;
using Project_CFT2.View;
using Project_CFT2.RFID;


using System.Configuration;
using System.IO;
using Multitouch.Framework.WPF;
using Multitouch.Framework.WPF.Input;

namespace Project_CFT2
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    /// 

    public delegate void showMemberPopup(object x, string name);

    public partial class Window3
    {
        rfid rfid = new rfid();
        int numItems = 0;
        int MAX_ITEMS = 15;
        double screen_width = SystemParameters.PrimaryScreenWidth;
        double screen_height = SystemParameters.PrimaryScreenHeight;  
        static System.Random randomGen = new System.Random();
//        FurniturePopupView furniturePopup;

        public Window3()
        {
            //Multitouch.Framework.WPF.Input.MultitouchScreen.AllowNonContactEvents = true;
            InitializeComponent();

            rfid.start(this);

            //FrameworkElement newView3;
            //newView3 = new FurniturePopupView();

            //furniturePopup = newView3 as FurniturePopupView;

            ////newView3.SetValue(Canvas.LeftProperty, this.Width - 320);
            ////newView3.SetValue(Canvas.TopProperty, this.Height/2 - 500/2-100);
            //newView3.SetValue(Canvas.LeftProperty, 965.0);
            //newView3.SetValue(Canvas.TopProperty, 100.0);
            //canvas1.Children.Add(newView3);
        }

        void Window_Loaded(object sender, RoutedEventArgs e)
        {

            //string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            string path =Directory.GetCurrentDirectory() + "/Resources/Furniture";
            
            LoadAllImages(path);
           // LoadButton();
        }



        void LoadAllImages(string folderName)
        {
            string[] fileNames = Directory.GetFiles(folderName);
            DirectoryInfo newDir = Directory.CreateDirectory(System.IO.Path.Combine(folderName, "small"));

            foreach (string fileName in fileNames)
            {
                if (IsImageExt(System.IO.Path.GetExtension(fileName)))
                {
                    if (numItems > MAX_ITEMS) break;
                    AddPhoto(fileName);
                    numItems++;
                }
            }
        }
        void LoadButton()
        {
            Multitouch.Framework.WPF.Controls.Button b = new Multitouch.Framework.WPF.Controls.Button();
            b.Height = 500;
            b.Width = 500;
            canvas1.Children.Add(b);
        }

        bool IsImageExt(string ext)
        {
            string[] exts = { ".jpg", ".png", ".gif", ".tiff", ".bmp", ".jpeg" };
            return exts.Contains(ext.ToLower());
        }

        void AddPhoto(string filePath)
        {
            BitmapImage bi = new BitmapImage(new Uri(filePath));

            FrameworkElement newView;

            double x = randomGen.Next(0, 700);
            double y = randomGen.Next(0, 1200);
            double a = randomGen.Next(0, 360);
            
            PhotoViewModel Photo = new PhotoViewModel(filePath, "50");

            newView = new PhotoView();
            newView.DataContext = Photo;

            TransformGroup trgrp = new TransformGroup();
            RotateTransform rotate = new RotateTransform(a,50.0,50.0);
            ScaleTransform scale = new ScaleTransform(1.0,2.0);
            
            trgrp.Children.Add(rotate);
            trgrp.Children.Add(scale);
            

           newView.SetValue(Canvas.TopProperty, x);
           newView.SetValue(Canvas.LeftProperty, y);
           newView.SetValue(Canvas.RenderTransformProperty, rotate);
           

           
           canvas1.Children.Add(newView);

           MultitouchScreen.AddContactEnterHandler(newView, ElementCaptureHandler);


        }

        private static void ElementCaptureHandler(object sender, ContactEventArgs e)
        {

            var element = sender as IInputElement;

            if (element != null)
            {
                e.Contact.Capture(element);


                //element.CaptureMouse();
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainWindow x = new MainWindow();
            //DynamicGeometry.Window1 x = new DynamicGeometry.Window1();
            
            x.Show();
            Close();
            //DynamicGeometry.Window1 w = new DynamicGeometry.Window1() as Window;
            //Window x = new DynamicGeometry.Window1();
            //testWindow x = new testWindow();
            //Window x = new Window();
            //Border x = new Border();
            
            
            //this.Close();
            //x.Show();

            
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MainWindow x = new MainWindow();
            //DynamicGeometry.Window1 x = new DynamicGeometry.Window1();

            x.Show();
            Close();

        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //MainWindow x = new MainWindow();
            //DynamicGeometry.Window1 x = new DynamicGeometry.Window1();
            WelcomePop.Visibility = Visibility.Visible;
            //x.Show();
            //Close();

        }
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //MainWindow x = new MainWindow();
            //DynamicGeometry.Window1 x = new DynamicGeometry.Window1();
            WelcomePop.Visibility = Visibility.Hidden;
            DrawLoad.Visibility = Visibility.Visible;
            //x.Show();
            //Close();

        }
        private void Window_Closed(object sender, System.EventArgs e)
        {
            rfid.stop();
        }
        public static void memberLogin(object x, string name)
        {
            var y = x as Window3;
            y.WelcomePop.memberNameTextBox.Text = "Welcome, " + name + "!";
            y.WelcomePop.Visibility = Visibility.Visible;
        }
    }
}
